<?php


namespace app\user\controller;


use think\Db;
use think\facade\Request;

class Event extends LoggingBase
{
    private function GetConfig($Type)
    {
        if (!$Type) {
            mb_OutMessage(ErrCode_Toast, '参数有误！');
        }
        $Config = null;
        switch ($Type) {
            case 'Client':
                $Config['Table'] = 'client';
                break;
            case 'User':
                $Config['Table'] = 'user';
                break;
            case 'Department':
                $Config['Table'] = 'company_department';
                break;
            case 'Post':
                $Config['Table'] = 'company_department_post';
                break;
            case 'UserCalls':
                $Config['Table'] = 'user_calls';
                break;
            case 'UserCallRecord':
                $Config['Table'] = 'user_call_records';
                break;
            case 'Contract':
                $Config['Table'] = 'contract';
                break;
            case 'CompanyFile':
                $Config['Table'] = 'company_file';
                break;
            case 'Receivables':
                $Config['Table'] = 'receivables';
                break;
            case 'GroupClient':
                $Config['Table'] = 'group_client';
                break;
            case 'ClientFollow':
                $Config['Table'] = 'client_follow';
                break;
            default:
                break;
        }
        return $Config;
    }

    /**
     * 删除
     */
    public function mb_Delete()
    {
        $Type = Request::has('Type') ? addslashes(Request::param('Type')) : '';
        $IDs = Request::has('IDs') ? addslashes(Request::param('IDs')) : 0;

        if (!$IDs) {
            mb_OutMessage(ErrCode_Toast, '参数有误！');
        }
        $Config = $this->GetConfig($Type);
        if (!$Config) {
            mb_OutMessage(ErrCode_Toast, '配置有误！');
        }
        Db::startTrans();
        try {
//            Db::table($Config['Table'])->where('ID','IN', $IDs)->update(['IsDeleted', 1]);
            $TableName = $Config['Table'];
            Db::execute("UPDATE $TableName SET IsDeleted='1' WHERE ID IN ($IDs);");
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal, '删除成功');
    }

    /**
     * 添加
     */
    public function mb_Add()
    {
        $Type = Request::has('Type') ? addslashes(Request::param('Type')) : '';
        $Params = Request::has('Params') ? Request::param('Params') : '';
        $Config = $this->GetConfig($Type);
        if (!$Config) {
            mb_OutMessage(ErrCode_Toast, '配置有误！');
        }
        $Params = json_decode($Params, true);
        Db::startTrans();
        try {
            Db::table($Config['Table'])->insert($Params);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal, '添加成功');
    }

    /**
     * 编辑
     */
    public function mb_Edit()
    {
        $Type = Request::has('Type') ? addslashes(Request::param('Type')) : '';
        $Params = Request::has('Params') ? Request::param('Params') : '';
        $Config = $this->GetConfig($Type);
        if (!$Config) {
            mb_OutMessage(ErrCode_Toast, '配置有误！');
        }
        $Params = json_decode($Params, true);
        Db::startTrans();
        try {
            Db::table($Config['Table'])->update($Params);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal, '保存成功');
    }
}